//
//  LJP_PlayerViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/10/6.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LJP_PlayerViewController.h"
#import "LJP_GuideCellModel.h"
#import "LJP_PlayerHeadView.h"
#import <AFNetworking.h>
#import "LJP_PlayerModel.h"
#import "LJP_PlayerDownView.h"

@interface LJP_PlayerViewController ()
@property (nonatomic, strong) LJP_PlayerHeadView *headView;
@property (nonatomic, strong) LJP_PlayerModel *playerModel;
@property (nonatomic, strong) LJP_PlayerDownView *downview;
@property (nonatomic, strong) Player *player;
@property (nonatomic, strong) UIScrollView *scroll;
@end

@implementation LJP_PlayerViewController

- (void)dataHandle
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?trackId=%@&device=android", self.headModel.trackId];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.playerModel = [[LJP_PlayerModel alloc] init];
        [self.playerModel setValuesForKeysWithDictionary:responseObject[@"userInfo"]];
        [self.playerModel setValue:responseObject[@"intro"] forKey:@"intro"];
        [self.playerModel setValue:responseObject[@"playUrl64"] forKey:@"playUrl64"];
        
        [self downViewSubView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.headView = [[LJP_PlayerHeadView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 430)];
    self.player = [Player share];
    [self dataHandle];
    
}
- (void)downViewSubView
{
    [self.scroll removeFromSuperview];
    CGRect rect = [self.playerModel.intro boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    self.downview = [[LJP_PlayerDownView alloc] initWithFrame:CGRectMake(0, 430, [UIScreen mainScreen].bounds.size.width, 130 + rect.size.height)];
    self.downview.model = self.playerModel;
    [self headViewSubView];
    self.headView.model = self.headModel;
    self.scroll = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.scroll];
    self.scroll.contentSize = CGSizeMake(0, 560 + rect.size.height);
    self.scroll.showsVerticalScrollIndicator = NO;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.pagingEnabled = YES;
    [self.scroll addSubview:self.headView];
    [self.scroll addSubview:self.downview];
    [self.player resume];
}
- (void)headViewSubView
{
    self.headView = [[LJP_PlayerHeadView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 430)];
    [self.headView.backButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.headView.sound addTarget:self action:@selector(soundClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView.playButton addTarget:self action:@selector(playButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView.nextButton addTarget:self action:@selector(nextButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView.lastButton addTarget:self action:@selector(lastButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)nextButtonClicked:(UIButton *)button
{
    if (self.player.num + 1 == self.ListArray.count) {
        self.headModel = self.ListArray[0];
    }else{
        self.headModel = self.ListArray[self.player.num + 1];
    }
    [self dataHandle];
    [self.player qiege:self.player.num + 1];
}
- (void)lastButtonClicked:(UIButton *)button
{
    if (self.player.num - 1 == 0) {
        self.headModel = self.ListArray[self.ListArray.count - 1];
        [self.player qiege:self.ListArray.count - 1];
    }else{
        self.headModel = self.ListArray[self.player.num - 1];
        [self.player qiege:self.player.num - 1];
    }
    [self dataHandle];
}
- (void)playButtonClicked:(UIButton *)button
{
    button.selected = !button.selected;
    if (button.selected) {
        [self.player pause];
    }else{
        [self.player resume];
    }
}
- (void)soundClicked:(UIButton *)button
{
    button.selected = !button.selected;
    if (button.selected) {
        [self.player mute];
    }else{
        [self.player unmute];
    }
}

- (void)backButtonClicked
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
